/*
* Copyright (c) 2009 contentismissing.net, artifacts.de
*
* All rights reserved.
*
* Authors: Jens Lembke <j@contentismissing.net>;
*          Michael Markowski <m.markowski@artifacts.de>;
*
* All non-exclusive rights granted to Deutsche Telekom AG Laboratories
*
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
/**
* DragBehaviour - Moves the object to the cursor position 
* Uses caurina tweener for moving.  
*/
package net.contentismissing.tlabs.cocktailparty.behaviours
{
	import de.artifacts.multitouch.Cursor;
	import de.artifacts.multitouch.TouchEvent;
	import de.artifacts.multitouch.behaviours.TwoFingerBehaviour;
	import flash.geom.Point;
	import net.contentismissing.tlabs.cocktailparty.model.Model;
	import net.contentismissing.tlabs.cocktailparty.model.TouchTapScroller;
	import net.contentismissing.tlabs.cocktailparty.ui.WhisperingArea;
	import net.contentismissing.tlabs.cocktailparty.ui.Workspace;
	public class TwoFingerWorkspaceBehaviour extends TwoFingerBehaviour
	{
		private var workspace : Workspace;
		private var cursor1 : Cursor;
		private var cursor2 : Cursor;
		public function TwoFingerWorkspaceBehaviour(workspace : Workspace)
		{
			super();
			this.workspace = workspace;
		}

		override public function touchedHandler(event : TouchEvent) : void
		{
			super.touchedHandler(event);
			if (this.touchingCursors.length == 1)
			{
				if (event.cursor.touchables.indexOf(touchable) > - 1)
				{
					var x : int = event.cursor.position.x;
					var y : int = event.cursor.position.y;
					var whisperingArea : WhisperingArea = Model.getInstance().whisperingArea;
					//var workspace : Workspace = Model.getInstance().currentWorkspace;
					if (whisperingArea != null && whisperingArea.isActive)
					{
						whisperingArea.whisperingAreaClose();
					}else
					{
						whisperingArea = new WhisperingArea();
						Model.getInstance().whisperingArea = whisperingArea;
						workspace.addChild(whisperingArea);
						workspace.setChildIndex(whisperingArea , 3);
						// 0 = workspace, 1 = background, 2 = head (midpoint), 3 = drüber		
						whisperingArea.startDrawingAt(new Point(x , y));
					}
				}
			}else if (this.touchingCursors.length > 1 && (Model.getInstance().whisperingArea != null && !Model.getInstance().whisperingArea.isActive) && !workspace.menu.menuFlag)
			{
				for each (var aCursor : Cursor in this.touchingCursors)
					aCursor.shouldSurpressInterpretationAsTap = true;
				cursor1 = this.touchingCursors[0];
				cursor2 = this.touchingCursors[1];
				(touchable.wrappee as TouchTapScroller).handleMouseDown(cursor2.position);
			}
		}

		override public function repositionedHandler(event : TouchEvent) : void
		{
			super.repositionedHandler(event);
			if (this.touchingCursors.length == 1)
			{
				if (event.cursor == null) return;
				var x : int = event.cursor.position.x;
				var y : int = event.cursor.position.y;
				if (Model.getInstance().whisperingArea != null) Model.getInstance().whisperingArea.drawTo(new Point(x , y));
				return ;
			}

			if (Model.getInstance().whisperingArea != null)
			{
				if (this.touchingCursors.length > 1 && !Model.getInstance().whisperingArea.isActive && !workspace.menu.menuFlag)
				{
					Model.getInstance().whisperingArea.clear();
					(touchable.wrappee as TouchTapScroller).handleMouseMove(cursor2.position);
				}
			}
		}

		override public function releasedHandler(event : TouchEvent) : void
		{
			super.releasedHandler(event);
			if (event.cursor == null) return;
			(touchable.wrappee as TouchTapScroller).handleMouseUp();
			if (Model.getInstance().whisperingArea != null) Model.getInstance().whisperingArea.stopDrawing();
			var workspace : Workspace = Model.getInstance().currentWorkspace;
			event.cursor = null;
		}
	}
}
